package com.ttbj.core.jpush;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ttbj.core.jpush.config.JpushConfig;
import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;

public class JpushClient {

	public final static Logger LOG = LoggerFactory.getLogger(JpushClient.class);

	public static void send(String content, String registId) {
		JPushClient jpushClient = new JPushClient(JpushConfig.MASTERSECRET, JpushConfig.APP_KEY, null,
				ClientConfig.getInstance());
		PushResult result;
		try {
			result = jpushClient.sendPush(buildPushObject_all_alias_alert(content,registId));
			LOG.info("Got result - " + result);
		} catch (APIConnectionException e) {
			e.printStackTrace();
			// Connection error, should retry later
			LOG.error("Connection error, should retry later", e);
		} catch (APIRequestException e) {
			// Should review the error, and fix the request
			LOG.error("Should review the error, and fix the request", e);
			LOG.info("HTTP Status: " + e.getStatus());
			LOG.info("Error Code: " + e.getErrorCode());
			LOG.info("Error Message: " + e.getErrorMessage());
		}

	}
	public static void sendAll(String content, List<String> registIds ) {
		JPushClient jpushClient = new JPushClient(JpushConfig.MASTERSECRET, JpushConfig.APP_KEY, null,
				ClientConfig.getInstance());
		PushResult result;
		try {
			result = jpushClient.sendPush(buildPushObject_all_alias_alert_all(content,registIds));
			LOG.info("Got result - " + result);
		} catch (APIConnectionException e) {
			e.printStackTrace();
			// Connection error, should retry later
			LOG.error("Connection error, should retry later", e);
		} catch (APIRequestException e) {
			// Should review the error, and fix the request
			LOG.error("Should review the error, and fix the request", e);
			LOG.info("HTTP Status: " + e.getStatus());
			LOG.info("Error Code: " + e.getErrorCode());
			LOG.info("Error Message: " + e.getErrorMessage());
		}

	}

	public static PushPayload buildPushObject_all_all_alert(String content) {
		return PushPayload.alertAll(content);
	}

	public static PushPayload buildPushObject_all_alias_alert(String content ,String registId) {
		return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.registrationId(registId))
                .setNotification(Notification.alert(content))
                .build();
	    }
	public static PushPayload buildPushObject_all_alias_alert_all(String content,List<String> registIds){
		return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.registrationId(registIds))
                .setNotification(Notification.alert(content))
                .build();
	}	
	
	

}
